package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.view.LayoutInflater;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencySaveCollectionListAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.SaveCollectionBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencySaveCollectionActivity extends CommonListActivity implements CallBack<DateBean> {
    private MySpinner spYear;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private List<String> strings;
    private int yearPostion;
    @Override
    public void initView() {
        super.initView();
        View itemView = LayoutInflater.from(this).inflate(R.layout.activity_emergency_save_collection_top, null);
        spYear = itemView.findViewById(R.id.spYear);
        strings = TimeUtils.getYearList();
        flContent.addView(itemView);


    }


    @Override
    public void initTitle() {
        super.initTitle();
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencySaveCollectionListAdapter(this);
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_SAVE_COLLECTION;
    }

    @Override
    public void configAdapter() {
        super.configAdapter();
    }

    @Override
    public void setListener() {
        super.setListener();
        Calendar cale = null;
        cale = Calendar.getInstance();
        int year = cale.get(Calendar.YEAR);
        spYear.getmText().setText(year+"");
        requestData(spYear.getmText().getText().toString());
        spYear.setmDatas(strings, new MySpinner.CallBack() {
            @Override
            public void onSelect(int postion) {
                yearPostion = postion;
                spYear.getmText().setText(strings.get(yearPostion));
                requestData(strings.get(yearPostion));
            }
        });
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(EmergencyCheck1Activity.class);//
            }
        });
//        spYear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                DatePickerUtil.getInstance(EmergencySaveCollectionActivity.this).setCallBack(EmergencySaveCollectionActivity.this);
//            }
//        });
    }

    private void requestData(String year) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_SAVE_COLLECTION);
        HashMap<String, String> map = new HashMap<>();
        map.put("year", year);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<SaveCollectionBean> saveCollectionBeanList = GsonUtil.jsonToList(listArray.toString(), SaveCollectionBean.class);
                mAdapter.updateItems(saveCollectionBeanList);


            }
        });
    }


    @Override
    public void onBack(DateBean dateBean) {
        spYear.getmText().setText(dateBean.getmYear()+"");
        requestData(dateBean.getmYear() + "");
    }
}
